package com.cheng.example.book.advise151.chap5;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

public class Client80 {
    public static void main(String[] args) {
        // 火车票列表
//        final List<String> tickets = new ArrayList<String>(100000);
        final List<String> tickets = new Vector<>(100000);
        // 初始化票据池
        for (int i = 0; i < 100000; i++) {
            tickets.add("火车票" + i);
        }
        // 退票
        Thread returnThread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    tickets.add("车票" + new Random().nextInt());
                }

            }

            ;
        };

        // 售票
        Thread saleThread = new Thread() {
            @Override
            public void run() {
                for (String ticket : tickets) {
                    tickets.remove(ticket);
                }
            }
        };
        // 启动退票线程
        returnThread.start();
        // 启动售票线程
        saleThread.start();
    }
}

class Foo80_Thsaf {

    public static void main(String[] args) {
        // 火车票列表
        List<String> tickets = new ArrayList<String>(100000);
        Set set = new CopyOnWriteArraySet();
        // 初始化票据池
        for (int i = 0; i < 100000; i++) {
            tickets.add("火车票" + i);
        }
        // 10个窗口售票
        for (int i = 0; i < 10; i++) {
            new Thread() {
                public void run() {
                    while (true) {
                        String rmString = tickets.remove(0);
                        System.out.println(Thread.currentThread().getId()
                                + "----" + rmString);
                        boolean f = set.add(rmString);
                        if (f == false) {
                            System.out.println("这货没有被删除\t" + rmString);
                        }
                        System.out.println("==" + set.size());
                        if (tickets.size() == 0) {
                            break;
                        }
                    }
                }

                ;
            }.start();
        }
    }
}
